//
//  HomeWebViewController.m
//  MemberProject
//
//  Created by mac on 15/7/7.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "HomeWebViewController.h"

@interface HomeWebViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property(strong,nonatomic)UIActivityIndicatorView *activityIndicatorView;
@end

@implementation HomeWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url=[NSURL URLWithString:self.urlStr];
    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
    self.webView.scalesPageToFit=YES;
    self.webView.delegate=self;
    
    self.activityIndicatorView=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [self.view addSubview:self.activityIndicatorView];
    self.activityIndicatorView.center=CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
    self.activityIndicatorView.activityIndicatorViewStyle=UIActivityIndicatorViewStyleGray;
    
    
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 25, 25);
    [button setImage:[UIImage imageNamed:@"new_back"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(chickLeftBarButtonItem:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:button];
    
//    button=[UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame=CGRectMake(0, 0, 25, 25);
//    [button setImage:[UIImage imageNamed:@"new_close"] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(chickCloseBarButtonItem:) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *closeBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItems=@[leftBarButtonItem];
    
    button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 25, 25);
    [button setImage:[UIImage imageNamed:@"new_refresh"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(chickRefreshBarButtonItem:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *refreshBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.rightBarButtonItem=refreshBarButtonItem;
}

-(void)chickLeftBarButtonItem:(UIBarButtonItem *)sender{
    if(self.webView.canGoBack){
        [self.webView goBack];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)chickCloseBarButtonItem:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)chickRefreshBarButtonItem:(UIBarButtonItem *)sender{
    [self.webView reload];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.view bringSubviewToFront:self.activityIndicatorView];
}

#pragma mark - webView delegate
- (void)webViewDidStartLoad:(UIWebView *)webView{
    //    [SVProgressHUD show];
    [self.activityIndicatorView startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    //    [SVProgressHUD dismiss];
    [self.activityIndicatorView stopAnimating];
    self.title=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    //    [SVProgressHUD showErrorWithStatus:@"网络错误"];
    [self.activityIndicatorView stopAnimating];
}


@end
